<!DOCTYPE html>
<html>
<head>
    <title>Advanced Find Tester</title>
    <script type="text/jscript" src="ClientGlobalContext.js.aspx"></script>

    <script type="text/jscript" src="../WebResources/pl_vkBeautify"></script>

    <link rel="stylesheet" href="../WebResources/pl_CodeMirrorCss">
    <link rel="stylesheet" href="../WebResources/pl_CodeMirrorShowHintCss">    
    <script src="../WebResources/pl_CodeMirror"></script>
    <script src="../WebResources/pl_CodeMirrorShowHint"></script>
    <script src="../WebResources/pl_CodeMirrorXmlHint"></script>
    <script src="../WebResources/pl_CodeMirrorXml"></script>     
    
    <style type="text/css">
        .CodeMirror {
            border-top: 1px solid black;
            border-bottom: 1px solid black;
            height: 465px;
            font-size: small;
        }
        h2 {
            font-family: Segoe UI,Tahoma,Arial;
            color: #262626;
            font-size: 27px;
            font-weight: lighter;
        }
    </style>
</head>
<body>
    <h2>Advanced Find Tester by: <a target="_blank" href="http://phuocle.net">phuocle.net</a></h2>
    <div style="padding-left:5px;">
        <form>
            <textarea id="fetchXml" name="fetchXml" rows="31" cols="94"></textarea><br />
            <input type="button" value="Execute" onclick="onExecute();" />&nbsp;&nbsp;
            <input type="button" value="Load Current FetchXml" onclick="loadCurrentFetchXml();" />&nbsp;&nbsp;
            <input type="button" value="Load Last Execute FetchXml" onclick="loadLastExecuteFetchXml();" />
        </form>
    </div>
    <script type="text/javascript">

        var tags = {
            "!top": [
              "value",
              "condition",
              "filter",
              "all-attributes",
              "fetch",
              "savedquery"
            ],
            "value": {},
            "condition": {
                "attrs": {
                    "column": null,
                    "attribute": null,
                    "entityname": null,
                    "operator": [
                      "eq",
                      "neq",
                      "ne",
                      "gt",
                      "ge",
                      "le",
                      "lt",
                      "like",
                      "not-like",
                      "in",
                      "not-in",
                      "between",
                      "not-between",
                      "null",
                      "not-null",
                      "yesterday",
                      "today",
                      "tomorrow",
                      "last-seven-days",
                      "next-seven-days",
                      "last-week",
                      "this-week",
                      "next-week",
                      "last-month",
                      "this-month",
                      "next-month",
                      "on",
                      "on-or-before",
                      "on-or-after",
                      "last-year",
                      "this-year",
                      "next-year",
                      "last-x-hours",
                      "next-x-hours",
                      "last-x-days",
                      "next-x-days",
                      "last-x-weeks",
                      "next-x-weeks",
                      "last-x-months",
                      "next-x-months",
                      "olderthan-x-months",
                      "last-x-years",
                      "next-x-years",
                      "eq-userid",
                      "ne-userid",
                      "eq-userteams",
                      "eq-useroruserteams",
                      "eq-businessid",
                      "ne-businessid",
                      "eq-userlanguage",
                      "this-fiscal-year",
                      "this-fiscal-period",
                      "next-fiscal-year",
                      "next-fiscal-period",
                      "last-fiscal-year",
                      "last-fiscal-period",
                      "last-x-fiscal-years",
                      "last-x-fiscal-periods",
                      "next-x-fiscal-years",
                      "next-x-fiscal-periods",
                      "in-fiscal-year",
                      "in-fiscal-period",
                      "in-fiscal-period-and-year",
                      "in-or-before-fiscal-period-and-year",
                      "in-or-after-fiscal-period-and-year",
                      "begins-with",
                      "not-begin-with",
                      "ends-with",
                      "not-end-with"
                    ],
                    "value": null,
                    "aggregate": [
                      "count",
                      "countcolumn",
                      "sum",
                      "avg",
                      "min",
                      "max"
                    ],
                    "alias": null,
                    "uiname": null,
                    "uitype": null,
                    "uihidden": [
                      "0",
                      "1"
                    ]
                },
                "children": [
                  "value"
                ]
            },
            "value": {
                "attrs": {
                    "uiname": null,
                    "uitype": null
                }
            },
            "filter": {
                "attrs": {
                    "type": [
                      "and",
                      "or"
                    ],
                    "isquickfindfields": null
                },
                "children": [
                  "condition",
                  "filter"
                ]
            },
            "all-attributes": {},
            "fetch": {
                "attrs": {
                    "version": null,
                    "count": null,
                    "page": null,
                    "paging-cookie": null,
                    "utc-offset": null,
                    "aggregate": null,
                    "distinct": null,
                    "top": null,
                    "mapping": [
                      "internal",
                      "logical"
                    ],
                    "min-active-row-version": null,
                    "output-format": [
                      "xml-ado",
                      "xml-auto",
                      "xml-elements",
                      "xml-raw",
                      "xml-platform"
                    ],
                    "returntotalrecordcount": null,
                    "no-lock": null
                },
                "children": [
                  "entity",
                  "order"
                ]
            },
            "entity": {
                "attrs": {
                    "name": null
                },
                "children": [
                  "all-attributes",
                  "attribute",
                  "order",
                  "link-entity",
                  "filter"
                ]
            },
            "attribute": {
                "attrs": {
                    "name": null,
                    "build": [
                      "1.504021",
                      "1.003017"
                    ],
                    "addedby": null,
                    "alias": null,
                    "aggregate": [
                      "count",
                      "countcolumn",
                      "sum",
                      "avg",
                      "min",
                      "max"
                    ],
                    "groupby": [
                      "true",
                      "false",
                      "1",
                      "0"
                    ],
                    "dategrouping": [
                      "day",
                      "week",
                      "month",
                      "quarter",
                      "year",
                      "fiscal-period",
                      "fiscal-year"
                    ],
                    "usertimezone": [
                      "true",
                      "false",
                      "1",
                      "0"
                    ]
                }
            },
            "order": {
                "attrs": {
                    "attribute": null,
                    "alias": null,
                    "descending": null
                }
            },
            "link-entity": {
                "attrs": {
                    "name": null,
                    "to": null,
                    "from": null,
                    "alias": null,
                    "link-type": null,
                    "visible": null,
                    "intersect": null
                },
                "children": [
                  "all-attributes",
                  "attribute",
                  "order",
                  "filter",
                  "link-entity"
                ]
            },
            "attribute": {
                "attrs": {
                    "name": null,
                    "build": [
                      "1.504021",
                      "1.003017"
                    ],
                    "addedby": null,
                    "alias": null,
                    "aggregate": [
                      "count",
                      "countcolumn",
                      "sum",
                      "avg",
                      "min",
                      "max"
                    ],
                    "groupby": [
                      "true",
                      "false",
                      "1",
                      "0"
                    ],
                    "dategrouping": [
                      "day",
                      "week",
                      "month",
                      "quarter",
                      "year",
                      "fiscal-period",
                      "fiscal-year"
                    ],
                    "usertimezone": [
                      "true",
                      "false",
                      "1",
                      "0"
                    ]
                }
            },
            "order": {
                "attrs": {
                    "attribute": null,
                    "alias": null,
                    "descending": null
                }
            },
            "link-entity": {
                "attrs": {
                    "name": null,
                    "to": null,
                    "from": null,
                    "alias": null,
                    "link-type": null,
                    "visible": null,
                    "intersect": null
                },
                "children": [
                  "all-attributes",
                  "attribute",
                  "order",
                  "filter",
                  "link-entity"
                ]
            },
            "order": {
                "attrs": {
                    "attribute": null,
                    "alias": null,
                    "descending": null
                }
            },
            "savedquery": {
                "attrs": {
                    "donotuseinLCID": null,
                    "useinLCID": null
                },
                "children": [
                  "name",
                  "savedqueryid",
                  "returnedtypecode",
                  "description",
                  "querytype",
                  "IsCustomizable",
                  "CanBeDeleted",
                  "IntroducedVersion",
                  "isquickfindquery",
                  "isuserdefined",
                  "isdefault",
                  "isprivate",
                  "queryapi",
                  "fetchxml",
                  "columnsetxml",
                  "layoutxml"
                ]
            },
            "name": {},
            "savedqueryid": {},
            "returnedtypecode": {
                "attrs": {
                    "formattedvalue": null
                }
            },
            "description": {},
            "querytype": {
                "attrs": {
                    "formattedvalue": null
                }
            },
            "IsCustomizable": {
                "attrs": {
                    "name": null
                }
            },
            "CanBeDeleted": {
                "attrs": {
                    "name": null
                }
            },
            "IntroducedVersion": {},
            "isquickfindquery": {
                "attrs": {
                    "name": null
                }
            },
            "isuserdefined": {
                "attrs": {
                    "name": null
                }
            },
            "isdefault": {
                "attrs": {
                    "name": null
                }
            },
            "isprivate": {},
            "queryapi": {},
            "fetchxml": {
                "children": [
                  "fetch"
                ]
            },
            "columnsetxml": {
                "children": [
                  "columnset"
                ]
            },
            "columnset": {
                "attrs": {
                    "version": null,
                    "distinct": null
                },
                "children": [
                  "column",
                  "ascend",
                  "descend",
                  "filter"
                ]
            },
            "column": {
                "attrs": {
                    "build": [
                      "1.504021",
                      "1.003017"
                    ],
                    "addedby": null
                }
            },
            "ascend": {},
            "descend": {},
            "filter": {
                "attrs": {
                    "column": null,
                    "operator": [
                      "eq",
                      "neq",
                      "ne",
                      "gt",
                      "ge",
                      "le",
                      "lt",
                      "like",
                      "not-like",
                      "in",
                      "not-in",
                      "between",
                      "not-between",
                      "null",
                      "not-null",
                      "yesterday",
                      "today",
                      "tomorrow",
                      "last-seven-days",
                      "next-seven-days",
                      "last-week",
                      "this-week",
                      "next-week",
                      "last-month",
                      "this-month",
                      "next-month",
                      "on",
                      "on-or-before",
                      "on-or-after",
                      "last-year",
                      "this-year",
                      "next-year",
                      "last-x-hours",
                      "next-x-hours",
                      "last-x-days",
                      "next-x-days",
                      "last-x-weeks",
                      "next-x-weeks",
                      "last-x-months",
                      "next-x-months",
                      "olderthan-x-months",
                      "last-x-years",
                      "next-x-years",
                      "eq-userid",
                      "ne-userid",
                      "eq-userteams",
                      "eq-useroruserteams",
                      "eq-businessid",
                      "ne-businessid",
                      "eq-userlanguage",
                      "this-fiscal-year",
                      "this-fiscal-period",
                      "next-fiscal-year",
                      "next-fiscal-period",
                      "last-fiscal-year",
                      "last-fiscal-period",
                      "last-x-fiscal-years",
                      "last-x-fiscal-periods",
                      "next-x-fiscal-years",
                      "next-x-fiscal-periods",
                      "in-fiscal-year",
                      "in-fiscal-period",
                      "in-fiscal-period-and-year",
                      "in-or-before-fiscal-period-and-year",
                      "in-or-after-fiscal-period-and-year",
                      "begins-with",
                      "not-begin-with",
                      "ends-with",
                      "not-end-with"
                    ],
                    "value": null,
                    "type": null
                },
                "children": [
                  "condition"
                ]
            },
            "condition": {
                "attrs": {
                    "column": null,
                    "operator": [
                      "eq",
                      "neq",
                      "ne",
                      "gt",
                      "ge",
                      "le",
                      "lt",
                      "like",
                      "not-like",
                      "in",
                      "not-in",
                      "between",
                      "not-between",
                      "null",
                      "not-null",
                      "yesterday",
                      "today",
                      "tomorrow",
                      "last-seven-days",
                      "next-seven-days",
                      "last-week",
                      "this-week",
                      "next-week",
                      "last-month",
                      "this-month",
                      "next-month",
                      "on",
                      "on-or-before",
                      "on-or-after",
                      "last-year",
                      "this-year",
                      "next-year",
                      "last-x-hours",
                      "next-x-hours",
                      "last-x-days",
                      "next-x-days",
                      "last-x-weeks",
                      "next-x-weeks",
                      "last-x-months",
                      "next-x-months",
                      "olderthan-x-months",
                      "last-x-years",
                      "next-x-years",
                      "eq-userid",
                      "ne-userid",
                      "eq-userteams",
                      "eq-useroruserteams",
                      "eq-businessid",
                      "ne-businessid",
                      "eq-userlanguage",
                      "this-fiscal-year",
                      "this-fiscal-period",
                      "next-fiscal-year",
                      "next-fiscal-period",
                      "last-fiscal-year",
                      "last-fiscal-period",
                      "last-x-fiscal-years",
                      "last-x-fiscal-periods",
                      "next-x-fiscal-years",
                      "next-x-fiscal-periods",
                      "in-fiscal-year",
                      "in-fiscal-period",
                      "in-fiscal-period-and-year",
                      "in-or-before-fiscal-period-and-year",
                      "in-or-after-fiscal-period-and-year",
                      "begins-with",
                      "not-begin-with",
                      "ends-with",
                      "not-end-with"
                    ],
                    "value": null
                }
            },
            "layoutxml": {
                "children": [
                  "grid"
                ]
            },
            "grid": {
                "attrs": {
                    "name": null,
                    "select": null,
                    "preview": null,
                    "icon": null,
                    "jump": null,
                    "object": null,
                    "disableInlineEditing": null,
                    "iconrenderer": null,
                    "multilinerows": null
                },
                "children": [
                  "row"
                ]
            },
            "row": {
                "attrs": {
                    "name": null,
                    "id": null,
                    "multiobjectidfield": null,
                    "layoutstyle": null
                },
                "children": [
                  "cell"
                ]
            },
            "cell": {
                "attrs": {
                    "name": null,
                    "width": null,
                    "LabelId": null,
                    "label": null,
                    "desc": null,
                    "ishidden": null,
                    "disableSorting": null,
                    "disableMetaDataBinding": null,
                    "cellType": null
                }
            }
        };


        function completeAfter(cm, pred) {
            var cur = cm.getCursor();
            if (!pred || pred()) setTimeout(function () {
                if (!cm.state.completionActive)
                    cm.showHint({ completeSingle: false });
            }, 100);
            return CodeMirror.Pass;
        }

        function completeIfAfterLt(cm) {
            return completeAfter(cm, function () {
                var cur = cm.getCursor();
                return cm.getRange(CodeMirror.Pos(cur.line, cur.ch - 1), cur) == "<";
            });
        }

        function completeIfInTag(cm) {
            return completeAfter(cm, function () {
                var tok = cm.getTokenAt(cm.getCursor());
                if (tok.type == "string" && (!/['"]/.test(tok.string.charAt(tok.string.length - 1)) || tok.string.length == 1)) return false;
                var inner = CodeMirror.innerMode(cm.getMode(), tok.state).state;
                return inner.tagName;
            });
        }

        var editor = CodeMirror.fromTextArea(document.getElementById("fetchXml"), {
            mode: "xml",
            height: "300px",
            lineNumbers: true,
            extraKeys: {
                "'<'": completeAfter,
                "'/'": completeIfAfterLt,
                "' '": completeIfInTag,
                "'='": completeIfInTag,
                "Ctrl-Space": "autocomplete"
            },
            hintOptions: { schemaInfo: tags }
        });

        function onExecute() {
            var fetchXml = editor.getValue();
            var oXmlDom = XUI.Xml.LoadXml(fetchXml);
            if (!IsNull(XUI.Xml.GetParserError(oXmlDom))) {
                alert("The XML passed to the platform is not well-formed XML");
                return;
            }
            var nodes = XUI.Xml.SelectNodes(oXmlDom, "/fetch/entity", null);
            if (nodes != null && nodes.length > 0) {
                ;
            }
            else {
                alert("Cannot find Entity Name in your FetchXML");
                return;
            }
            Mscrm.Utilities.setReturnValue(fetchXml);
            if (localStorage) {
                localStorage.setItem('LastExecuteFetchXml', fetchXml);
            }
            closeWindow(true);
        }
        function loadCurrentFetchXml() {
            if (localStorage) {
                editor.setValue(vkbeautify.xml(localStorage.getItem("CurrentFetchXml"), 2));
            }
            else {
                alert('Cannot load current FetchXml.');
            }
        }
        function loadLastExecuteFetchXml() {
            if (localStorage) {
                editor.setValue(vkbeautify.xml(localStorage.getItem("LastExecuteFetchXml"), 2));
            }
            else {
                alert('Cannot load last execute FetchXml.');
            }
        }
    </script>
</body>
</html>